﻿#region

using Microsoft.AspNetCore.Mvc;
using Vin.AspNetCore.Core.Controller;
using Vin.Extension.Framework.Login.Attributes;
using Vin.Extension.Framework.Login.ServiceInterface;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Attributes;
using Vin.Tool.Core.EnumCategory;

#endregion

namespace Vin.Extension.Framework.Login.Controller.Controllers;

public class SysLoginController : VinBaseController
{
    [Inject] private readonly ISysLoginService _sysLoginService = null!;

    /**
     * 登录逻辑
     * 1. 输入账号密码,登录后获取token
     * 2. 获取当前账号下的员工信息，如果是多个，需要选择一个员工，进入系统，如果只有一个员工，直接进入系统
     */
    // 登录逻辑可修改为 登录接口给一个token，员工登录的时候切换新的token，如果只有一个员工，直接返回最新的token
    [HttpPost("/login")]
    public async Task<IActionResult> Login([FromHeader(Name = "Type")] string? type)
    {
        type ??= VinLoginType.Password.GetName();
        var handler = VinApp.GetRequiredKeyServiceByHttpContext<ILoginService>(type);
        return Success(await handler.LoginAsync(HttpContext));
    }

    [HttpGet("/verify")]
    public async Task<IActionResult> Captcha([FromHeader(Name = "Type")] string? type)
    {
        type ??= VinLoginType.Password.GetName();
        var handler = VinApp.GetRequiredKeyServiceByHttpContext<ILoginService>(type);
        return Success(await handler.VerifyAsync(HttpContext));
    }

    [HttpGet("/login-ways")]
    public IActionResult LoginWays()
    {
        return Success(Vin.Extension.Framework.Login.ExtensionEntry.LoginWays.Where(m => !m.Default).Select(m => new
        {
            Name = m.LoginType,
            Type = m.LoginHandleType,
            m.Way,
            m.Icon,
            m.CanBind,
            m.Default
        }));
    }

    [HttpPost("/logout")]
    public async Task<IActionResult> Logout()
    {
        await _sysLoginService.LogoutAsync();
        return Success();
    }
}